package com.stores.action;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.JDOUserException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.appengine.api.datastore.KeyFactory;
import com.stores.model.Address;
import com.stores.model.Db;
import com.stores.model.PhoneNumber;
import com.stores.model.Store;
import com.stores.viewModel.StoreViewModel;

public class ManageStoresAction {	

	/**
	 * 
	 * @param category
	 */
	public void Create(Store store) {
		PersistenceManager pm = Db.getPersistenceManager();
		try {
			pm.makePersistent(store);
		} finally {
			pm.close();
		}
	}

	/**
	 * 
	 * @param category
	 * @return
	 */
	public boolean Update(Store store, long id) {
		PersistenceManager pm = Db.getPersistenceManager();

		try {
			Store storeData = pm.getObjectById(Store.class,
					KeyFactory.createKey(Store.class.getSimpleName(), id));
			storeData.setName(store.getName());

			// storeData.setStoreAddress(store.getStoreAddress());
			Address addressData = storeData.getStoreAddress();
			if (addressData == null) {
				addressData = new Address();
				storeData.setStoreAddress(addressData);
			}
			Address address = store.getStoreAddress();
			if (address != null) {
				addressData.setBuildingOrPlot(address.getBuildingOrPlot());
				addressData.setCity(address.getCity());
				addressData.setCountry(address.getCountry());
				addressData.setLine1(address.getLine1());
				addressData.setLine2(address.getLine2());
				addressData.setLocality(address.getLocality());
			}
			// storeData.setStoreNumber(store.getStoreNumber());
			PhoneNumber phoneData = storeData.getStoreNumber();
			if (phoneData == null) {
				phoneData = new PhoneNumber();
				storeData.setStoreNumber(phoneData);
			}
			PhoneNumber phone = store.getStoreNumber();
			if (phone != null) {
				phoneData.setNumber(phone.getNumber());
			}
			storeData.setDescription(store.getDescription());
			storeData.setEmailAddress(store.getEmailAddress());
			storeData.setContactPersonName(store.getContactPersonName());
			storeData.setTagline(store.getTagline());
			storeData.setTin(store.getTin());
		} finally {
			pm.close();
		}
		return true;
	}

	/***
	 * 
	 * @param id
	 * @return
	 */
	public StoreViewModel Retrieve(long id) {
		PersistenceManager pm = Db.getPersistenceManager();
		try {
			Store result = pm.getObjectById(Store.class,
					KeyFactory.createKey(Store.class.getSimpleName(), id));
			if (result == null)
				return null;
			StoreViewModel model = new StoreViewModel();
			model.setName(result.getName());
			Address address = result.getStoreAddress();
			PhoneNumber phone = result.getStoreNumber();
			if (address != null) {
				model.setBuilding(address.getBuildingOrPlot());
				model.setCity(address.getCity());
				model.setHouse(address.getLine1());
				model.setLocality(address.getLocality());
				model.setStreet(address.getLine2());
			}
			if (phone != null) {
				model.setPhoneNumber(phone.getNumber());
			}
			model.setContactPerson(result.getContactPersonName());
			model.setDescription(result.getDescription());
			model.setEmail(result.getEmailAddress());
			model.setTagline(result.getTagline());
			model.setTin(result.getTin());
			return model;
		} finally {
			pm.close();
		}
	}

	/**
	 * 
	 * @return
	 */
	public List<StoreViewModel> Retrieve() {
		PersistenceManager pm = Db.getPersistenceManager();
		Query query = pm.newQuery(Store.class);
		try {
			Object queryResult = null;
			try {
				queryResult = query.execute();
			} catch (JDOUserException ex) {
				return null;
			}

			@SuppressWarnings("unchecked")
			List<Store> stores = (List<Store>) queryResult;
			List<StoreViewModel> result = new ArrayList<StoreViewModel>();
			for (Store store : stores) {
				StoreViewModel model = new StoreViewModel();
				model.setStoreId(store.getKey().getId());
				PhoneNumber phone = store.getStoreNumber();
				Address address = store.getStoreAddress();
				if (phone != null) {
					model.setPhoneNumber(phone.getNumber());
				}
				if (address != null) {
					model.setLocality(address.getLocality());
				}
				model.setName(store.getName());
				model.setContactPerson(store.getContactPersonName());
				result.add(model);
			}
			return result;
		} finally {
			query.closeAll();
		}
	}
}
